using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.Office.Interop.Outlook;
using System.Diagnostics;
using System.Reflection;
using System.Collections;

namespace TXLooker.WorkItemExtention.Core
{
    internal class WIStoredQuery : WIObject
    {
        public WIStoredQuery( WIQueriesGroup parent, StoredQuery query ) : base(parent)
        {
            this.QueryGuid = query.QueryGuid;
            this.QueryName = query.Name;
            this.QueryScope = query.QueryScope;
            this.InitFolder( this.Parent.Folder ); 
        }

        private WIQueriesGroup QueriesGroup
        {
            get { return (this.Parent as WIQueriesGroup); }
        }

        public Uri ProjectUri
        {
            get { return this.QueriesGroup.ProjectUri; }
        }

        public string ProjectName
        {
            get { return this.QueriesGroup.ProjectName; }
        }

        public WIProjectProvider ProjectProvider
        {
            get { return this.QueriesGroup.ProjectProvider; }
        }

        public Project GetProject()
        {
            return this.QueriesGroup.GetProject();
        }

        /// <summary>
        /// 
        /// </summary>
        public Guid QueryGuid { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        public QueryScope QueryScope { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        public string QueryName { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        public StoredQuery GetQuery()
        {
            StoredQuery query = null;

            try
            {
                WorkItemStore store = this.GetStore();

                if( store != null )
                {
                    query = store.GetStoredQuery( this.QueryGuid );
                }
            }
            catch( System.Exception ex )
            {
                Debug.WriteLine( ex.ToString() );
            }

            return query;
        }

        public override string FolderName
        {
            get { return this.QueryName; }
        }

        public override WIObjectType Type
        {
            get { return WIObjectType.Query; }
        }


        public WorkItemCollection ExecQuery()
        {
            WorkItemCollection result = null;

            try
            {
                StoredQuery query = this.GetQuery();
                string queryText = query.QueryText.Replace( "@project", string.Format("'{0}'",query.Project.Name) );
                result = query.Project.Store.Query( queryText );
            }
            catch
            { }

            return result;
        }

        public override OlFolderView FolderView
        {
            get { return this.m_folderView; }
            set 
            {
                if( value != null )
                {
                    if( value.GetType() != typeof( OlWIQueryFolderView ) )
                        throw new ArgumentException();
                }
                this.m_folderView = value; 
            }
        }
        private OlFolderView m_folderView = null;

        protected override void UpdateData()
        {
            try
            {
                StoredQuery query = this.GetQuery();
                this.QueryName = query.Name;
                this.Folder.Name = this.FolderName;
            }
            catch
            { }
        }

        public bool NeedRefresh { get; set; }
    }
}
